{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import re\n",
    "sent='[中央/n  人民/n  广播/vn  电台/n]nt  、/w  [中国/ns  国际/n  广播/vn  电台/n]nt  和/c  [中央/n  电视台/n]nt  ，/w  向/p  全国/n  各族/r  人民/n  ，/w  向/p  [香港/ns  特别/a  行政区/n]ns  同胞/n  、/w  澳门/ns  和/c  台湾/ns  同胞/n  、/w  海外/s  侨胞/n  ，/w  向/p  世界/n  各国/r  的/u  朋友/n  们/k  ，/w  致以/v  诚挚/a  的/u  问候/vn  和/c  良好/a  的/u  祝愿/vn  ！/w'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'[中央/n  人民/n  广播/vn  电台/n]nt  、/w  [中国/ns  国际/n  广播/vn  电台/n]nt  和/c  [中央/n  电视台/n]nt  ，/w  向/p  全国/n  各族/r  人民/n  ，/w  向/p  [香港/ns  特别/a  行政区/n]ns  同胞/n  、/w  澳门/ns  和/c  台湾/ns  同胞/n  、/w  海外/s  侨胞/n  ，/w  向/p  世界/n  各国/r  的/u  朋友/n  们/k  ，/w  致以/v  诚挚/a  的/u  问候/vn  和/c  良好/a  的/u  祝愿/vn  ！/w'"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tokens=sent.split('  ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['[中央/n',\n",
       " '人民/n',\n",
       " '广播/vn',\n",
       " '电台/n]nt',\n",
       " '、/w',\n",
       " '[中国/ns',\n",
       " '国际/n',\n",
       " '广播/vn',\n",
       " '电台/n]nt',\n",
       " '和/c',\n",
       " '[中央/n',\n",
       " '电视台/n]nt',\n",
       " '，/w',\n",
       " '向/p',\n",
       " '全国/n',\n",
       " '各族/r',\n",
       " '人民/n',\n",
       " '，/w',\n",
       " '向/p',\n",
       " '[香港/ns',\n",
       " '特别/a',\n",
       " '行政区/n]ns',\n",
       " '同胞/n',\n",
       " '、/w',\n",
       " '澳门/ns',\n",
       " '和/c',\n",
       " '台湾/ns',\n",
       " '同胞/n',\n",
       " '、/w',\n",
       " '海外/s',\n",
       " '侨胞/n',\n",
       " '，/w',\n",
       " '向/p',\n",
       " '世界/n',\n",
       " '各国/r',\n",
       " '的/u',\n",
       " '朋友/n',\n",
       " '们/k',\n",
       " '，/w',\n",
       " '致以/v',\n",
       " '诚挚/a',\n",
       " '的/u',\n",
       " '问候/vn',\n",
       " '和/c',\n",
       " '良好/a',\n",
       " '的/u',\n",
       " '祝愿/vn',\n",
       " '！/w']"
      ]
     },
     "execution_count": 157,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 158,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "content_regex=re.compile('\\s\\[.+?/.+?\\]')\n",
    "tag_regex=re.compile('\\](\\w+)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "contents=re.findall(regex,sent)\n",
    "tags=re.findall(tag_regex,sent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('中央/n  人民/n  广播/vn  电台/n', '中央', 'n  人民/n  广播/vn  电台/n'),\n",
       " ('中国/ns  国际/n  广播/vn  电台/n', '中国', 'ns  国际/n  广播/vn  电台/n'),\n",
       " ('中央/n  电视台/n', '中央', 'n  电视台/n'),\n",
       " ('香港/ns  特别/a  行政区/n', '香港', 'ns  特别/a  行政区/n')]"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "contents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['nt', 'nt', 'nt', 'ns']"
      ]
     },
     "execution_count": 162,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "中央/n\n",
      "2\n",
      "\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "substring not found",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-170-cba8ff06d2a3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      4\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mt\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mcontent\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msplit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m' '\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m         \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'/'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      7\u001b[0m     \u001b[0mmerges\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmerge\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[1;31m# for i in range(len(merges)):\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: substring not found"
     ]
    }
   ],
   "source": [
    "merges = []\n",
    "for content in contents:\n",
    "    merge = ''\n",
    "    for t in content[0].split(' '):\n",
    "        print(t)\n",
    "    merges.append(merge)\n",
    "# for i in range(len(merges)):\n",
    "#     merges[i]=merges[i]+'/'+tags[i]\n",
    "# print(merges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 149,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 ['[中国/ns', '政府/n]nt']\n",
      "6 7 ['[日本/ns', '小国家/n]nt']\n"
     ]
    }
   ],
   "source": [
    "groups=[]\n",
    "first_index=-1\n",
    "last_index=-1\n",
    "i=0\n",
    "j=0\n",
    "while i<len(tokens):\n",
    "    t=tokens[i]\n",
    "    if first_index==-1 and t!='' and '['==t[0] :#找到块的第一个\n",
    "        first_index=i\n",
    "    if first_index!=-1 and t.count(']')!=0:\n",
    "        last_index=i\n",
    "        print(first_index,last_index,tokens[first_index:last_index+1])\n",
    "        groups.append((first_index,last_index))\n",
    "        i=last_index\n",
    "        first_index=-1\n",
    "        last_index=-1\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s='[asdada adasd /asd'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.count(']')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "for t in tokens:\n",
    "    print(t=='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#更新\n",
    "merges.reverse()\n",
    "for i, g in enumerate(groups[::-1]):\n",
    "    a, b = g\n",
    "    tokens[a:b + 1] = [merges[i]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['19980101-01-001-014/m',\n",
       " '中国政府/nt',\n",
       " '将/d',\n",
       " '继续/v',\n",
       " '坚持/v',\n",
       " '日本小国家/nt',\n",
       " '政策/n',\n",
       " '']"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
